<?php
$this->Html->addCrumb('Newsletters', '/member/newsletters');
$this->Html->addCrumb('Edit');
$this->Html->scriptStart(array('inline' => false));
echo '
      $(function(){
        $("#NewsletterMemberEditForm").TWS_Form( {
          validate : {
          rules : {
            NewsletterTitle : {required : true, help : "This is the title for the newsletter"},
            NewsletterFilePath: {required : true, help : "Select the newsletter to be uploaded"},
            NewsletterNewsletterDate : {required : true, date:true, help : "This is the date when the newsletter was published. It will assist in determining the sort order when newsletters are displayed. The most recent newsletters are displayed first."}
          }
          }});
         $("#deleteBtn").click(function(event){
          if(!$("#deleteBtn").attr("data-delete-confirm")) {
          $("#deleteBtn").attr("data-delete-confirm","");
            $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this record?", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); $("#deleteBtn").attr("data-delete-confirm",true).trigger("click");}}}});
            event.preventDefault();
          }
        });
         $("#deleteFile").click(function(event){
            var $link = $(this);
            $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this file?", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); location.href=$link.attr("href");}}}});
            event.preventDefault();
        });
      });
       ';
$this->Html->scriptEnd();

?>

<h1>Edit Newsletter</h1>
<?php echo $this->Session->flash();?>
<?php echo '<div id="actionLink">'.$this->Html->link('Back to list of Newsletters',array('action'=>'index', 'member'=>true)).'</div>'; ?>

<?php echo $this->element('errors', array('errors' => isset($errors)? $errors : null)); ?>
<?php

echo $this->TWSForm->create('Newsletter',  array(
    'inputDefaults' => array(
        'div' => array('class'=>'row')),
        'class'=>'inlineForm',
        'type'=>'file'));
echo $this->TWSForm->input('title', array('name'=>'NewsletterTitle', 'type'=>'text', 'size'=>'65','label'=>'<span class="required">*</span>Title'));

$record = $this->request->data['Newsletter'];
if(!empty($record['file_path'])){
  echo '<p><a href="'.$this->Html->url('/'.$record['file_path']).'" target="new">'.$this->Html->image('documentSmall.png', array("alt"=>"Newsletter")).'</a><br/>'.substr($record['file_path'], strrpos($record['file_path'], "/")+1).'</p>';
  echo '<p>'.$this->Html->link('Delete newsletter', array('controller' => 'newsletters', 'action' => 'deletefile', $record['id']), array("title"=>"Delete this newsletter" , "id"=>"deleteFile")).'</p>';
  echo $this->TWSForm->input('_file_path', array('type'=>'hidden', 'name'=>'_file_path', 'value'=>$record['file_path']));
  } else {
  echo $this->TWSForm->input('file_path', array('type'=>'file', 'label'=>'<span class="required">*</span>Upload newsletter', 'name'=>'NewsletterFilePath'));
}
echo $this->TWSForm->input('newsletter_date', array('type'=>'text', 'rel'=>'date', 'label'=>'<span class="required">*</span>Newsletter date', 'name'=>'NewsletterNewsletterDate'));
echo $this->TWSForm->submit('Update', array('id'=>'editBtn', 'name'=>'editBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->submit("Delete", array('id'=>'deleteBtn','name'=>'deleteBtn', 'class'=>'button', 'div'=>false));

echo $this->TWSForm->end();

?>